/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TexturEditor.java
 *
 * Created on 04.04.2012, 19:28:48
 */
package Editoren;

import Schnittstellen.Graphisches.Texturen.BildTextur;
import Schnittstellen.Graphisches.Texturen.Textur;
import Schnittstellen.ObjectManager;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.filechooser.FileNameExtensionFilter;

/**
 *
 * @author geez
 */
public class TextureEditor extends javax.swing.JPanel {

    ObjectManager om = ObjectManager.getInstance();
    JFrame chooserframe;
    JFileChooser filechooser;

    /** Creates new form TexturEditor */
    public TextureEditor() {
        initComponents();
        setupChooser();
    }

    private void setupChooser() {
        chooserframe = new JFrame("laden..");
        filechooser = new JFileChooser();
        filechooser.setMultiSelectionEnabled(true);
        filechooser.setAcceptAllFileFilterUsed(false);
        filechooser.addChoosableFileFilter(new FileNameExtensionFilter("Images", "jpg", "bmp", "gif", "png", "jpeg"));
        filechooser.addChoosableFileFilter(new FileNameExtensionFilter("Texturen", "dds", "fpn", "hdr", "tga"));
        filechooser.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                if (e.getActionCommand().matches(JFileChooser.APPROVE_SELECTION)) {
                    if (filechooser.getSelectedFiles().length > 1) {
                        for (File f : filechooser.getSelectedFiles()) {
                            importTextures(f);
                        }
                    } else if (filechooser.getSelectedFile() != null) {
                        importTextures(filechooser.getSelectedFile());
                    }
                    Lliste.updateUI();
                    chooserframe.setVisible(false);
                }
            }
        });
        chooserframe.add(filechooser);
        chooserframe.pack();
        chooserframe.setResizable(false);
    }

    private void importTextures(File f) {
        om.addToAssets(ObjectManager.Dateityp.TEXTUR, f);
        om.addTextur(new BildTextur(f.getName()));
    }

    private void showTexture(BildTextur t) {
        ((ImagePanel) Ptexture).setImg(t.getBufferedImage());
        Ptexture.repaint();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        Lliste = new javax.swing.JList();
        Bremove = new javax.swing.JButton();
        Badd = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        Ptexture = new ImagePanel();

        setLayout(new java.awt.BorderLayout());

        jPanel2.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        Lliste.setModel(new javax.swing.AbstractListModel() {
            public int getSize() { return om.getTexturbank().size(); }
            public Object getElementAt(int i) { return om.getTexturbank().get(i); }
        });
        Lliste.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                LlisteValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(Lliste);

        jPanel2.add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(87, 55, 130, 360));

        Bremove.setText("-");
        Bremove.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BremoveActionPerformed(evt);
            }
        });
        jPanel2.add(Bremove, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 20, 40, -1));

        Badd.setText("+");
        Badd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BaddActionPerformed(evt);
            }
        });
        jPanel2.add(Badd, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 20, 40, -1));

        add(jPanel2, java.awt.BorderLayout.EAST);

        Ptexture.setVerifyInputWhenFocusTarget(false);

        javax.swing.GroupLayout PtextureLayout = new javax.swing.GroupLayout(Ptexture);
        Ptexture.setLayout(PtextureLayout);
        PtextureLayout.setHorizontalGroup(
            PtextureLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 626, Short.MAX_VALUE)
        );
        PtextureLayout.setVerticalGroup(
            PtextureLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 436, Short.MAX_VALUE)
        );

        jScrollPane2.setViewportView(Ptexture);

        add(jScrollPane2, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

private void BaddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BaddActionPerformed
    chooserframe.setVisible(true);
}//GEN-LAST:event_BaddActionPerformed

private void BremoveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BremoveActionPerformed
    om.removeFromAssets(ObjectManager.Dateityp.TEXTUR, (Textur) Lliste.getSelectedValue());
    Lliste.updateUI();
}//GEN-LAST:event_BremoveActionPerformed

private void LlisteValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_LlisteValueChanged
    if (Lliste.getSelectedValue() != null) {
        showTexture((BildTextur) Lliste.getSelectedValue());
    }
}//GEN-LAST:event_LlisteValueChanged
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Badd;
    private javax.swing.JButton Bremove;
    private javax.swing.JList Lliste;
    private javax.swing.JPanel Ptexture;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    // End of variables declaration//GEN-END:variables

    class ImagePanel extends JPanel {

        private BufferedImage img;

        @Override
        protected void paintComponent(Graphics g) {
            if (img != null) {
                g.drawImage(img, 0, 0, this);
            }
        }

        public void setImg(BufferedImage img) {

            this.img = img;
            this.setSize(img.getWidth(), img.getHeight());
        }
    }
}
